<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Cart_model extends CI_Model {
	
	public function __construct(){
		
		parent::__construct();

	}
	
	/*
	 * 添加产品到购物车
	* */
	public function insert_cart($proid='',$proimg=''){
	
		$all_seesion_data = $this->session->all_userdata();
	
		$query = $this->db->get_where('products',array('pro_id'=>$proid));
	
		$pro_info = $query->row_array();
	
		$qty = 1;
	
		$product_key = $pro_info['pro_id'];
	
		if(array_key_exists('product',$all_seesion_data)&&$all_seesion_data['product'] != NULL){
	
			foreach ($all_seesion_data['product'] as $key => $value){
	
				if ($key == $product_key){
	
					$qty += $value['qty'];
	
					break;
				}
					
				$qty = $qty;
	
			}
		}
			
		$data = array(
				'qty' => $qty,
				'pro_id' => $product_key,
				'price' => $pro_info['price'],
				'proname' => $pro_info['proname'],
				'prourl' => $pro_info['prourl'],
				'img' => $proimg
		);
	
		if(array_key_exists('product',$all_seesion_data)){
	
			$data_add = $all_seesion_data['product'];
	
			$data_add[$product_key] = $data;
				
		}else{
	
			$data_add[$product_key] = $data;
		}
			
		$this->session->set_userdata('product',$data_add);
	
	}
	
	/*
	 * 从购物车删除产品
	 * */
	public function cartdelete($product_id = ''){
	
		if($product_id){
				
			$all_seesion_data = $this->session->all_userdata();
	
			$arr = $all_seesion_data['product'];
	
			unset($arr[$product_id]);
	
			$this->session->set_userdata('product',$arr);
				
			return true;
				
		}else{
				
			return FALSE;
		}
	}
	
	/*
	 * 改变产品数量
	 * */
	public function changeqty(){
	
		$product_id = $this->input->post('product_id');
	
		$qty = $this->input->post('qty');
	
		$all_seesion_data = $this->session->all_userdata();
			
		$data = $all_seesion_data['product'];
			
		$data[$product_id]['qty'] = $qty;
			
		$this->session->set_userdata('product',$data);
	}
	
	/*
	 * 支付信息
	 * */
	public function get_payinfo($msg = array()){

		$orderid = $msg['orderid'];
		$siteid = '933738101';
		$key = 'DPRR29NV6YLE';	
		$signature = md5($orderid.$siteid.$key);
	
		$data = array(
				'siteno' => $siteid,
				'orderid' => $orderid,
				'currency' => 'USD',
				'language' => 'en',
				'vat' => 0,
				'discount' => 0,
				'ShippingFee' => $msg['shippingfee'],
				'productno' => $productno,
				'shipfirstname' => $msg['ShippingFirstName'],
				'shiplastname' => $msg['ShippingLastName'],
				'shipaddr' => $msg['ShippingAddress'],
				'shipcity' => $msg['ShippingCity'],
				'shipstate' => $msg['ShippingState'],
				'shippostcode' => $msg['ShippingZipcode'],
				'shipcountry' => $msg['ShippingCountry'],
				'email' => $msg['email'],
				'shiptel' => $msg['ShippingTelephone'],
				'verifyCode' => $signature,
				'reUrl' => site_url().'user_mag',
				'notifyUrl' => site_url().'return_page'
		);
			
		$i = 0;
		foreach ($msg['product'] as $k=>$v){
			$data['productno['.$i.']'] = $v['product_id'];
			$data['productname['.$i.']'] = $v['product_name'];
			$data['priceunit['.$i.']'] = $v['product_price'];
			$data['quantity['.$i.']'] = $v['qty'];
			$i++;
		}
	
		return $data;
			
	}
	
	
}